#include <iostream>
#include <SFML/Graphics.hpp>
#include "../../include/Engine.hpp"
#include "../../include/GUI/Widget.hpp"

using namespace std;

namespace GUI
{
    Widget::Widget(Engine &Moteur) : m_Moteur(Moteur)
    {
        m_position.x = 0;
        m_position.y = 0;
        m_padding = m_Moteur.Config()->GetGUI()->padding;
        m_conteneur_position.x = 0;
        m_conteneur_position.y = 0;
        m_dimensions.width = 0;
        m_dimensions.height = 0;
        m_parent = NULL;
        m_deplacement = false;

        m_open = true;
    }

    void Widget::SetPosition(const int x, const int y)
    {
        m_position.x = x;
        m_position.y = y;
    }
    void Widget::SetPositionX(const int x)
    {
        m_position.x = x;
    }
    void Widget::SetPositionY(const int y)
    {
        m_position.y = y;
    }
    void Widget::SetConteneurPosition(const int x, const int y)
    {
        m_conteneur_position.x = x;
        m_conteneur_position.y = y;
    }
    void Widget::SetConteneurPositionX(const int x)
    {
        m_conteneur_position.x = x;
    }
    void Widget::SetConteneurPositionY(const int y)
    {
        m_conteneur_position.y = y;
    }
    void Widget::SetDimensions(const unsigned int width, const unsigned int height)
    {
        m_dimensions.width = width;
        m_dimensions.height = height;
    }

    bool Widget::IsHover()
    {
        int x = m_Moteur.App().GetInput().GetMouseX();
        int y = m_Moteur.App().GetInput().GetMouseY();

        if(x >= m_position.x and y >= m_position.y and x < m_position.x+m_dimensions.width+2*m_padding and y < m_position.y+m_dimensions.height+2*m_padding)
            return true;
        else
            return false;
    }
    bool Widget::IsClicked()
    {
        if(m_Moteur.Event()->Type == sf::Event::MouseButtonPressed and m_Moteur.App().GetInput().IsMouseButtonDown(sf::Mouse::Left) and !m_Moteur.App().GetInput().IsMouseButtonDown(sf::Mouse::Right))
        {
            if(IsHover())
                return true;
            else
                return false;
        }
        else
            return false;
    }
    bool Widget::IsMove()
    {
        return m_deplacement;
    }
    void Widget::SetParent(Widget *parent)
    {
        m_parent = parent;
    }
    void Widget::OpenWidget(Widget *widget)
    {

    }
    void Widget::CloseWidget(Widget *widget)
    {

    }
    void Widget::Open()
    {
        if(m_parent != NULL and !m_open)
        {
            m_parent->OpenWidget(this);
        }
        m_open = true;
    }
    void Widget::Close()
    {
        if(m_parent != NULL and m_open)
        {
            m_parent->CloseWidget(this);
        }
        m_open = false;
    }

    void Widget::Center()
    {
        if(m_parent != NULL)
        {
            SetConteneurPositionX((m_parent->GetDimensions().width - m_dimensions.width - 2*m_padding)/2);
            SetPositionX(m_parent->GetPosition().x + m_conteneur_position.x + m_padding);
        }
    }

    bool Widget::IsOpen()
    {
        return m_open;
    }
    Dimensions Widget::GetDimensions() const
    {
        return m_dimensions;
    }
    Position Widget::GetPosition() const
    {
        return m_position;
    }
    Position Widget::GetConteneurPosition() const
    {
        return m_conteneur_position;
    }
    int Widget::GetPadding() const
    {
        return m_padding;
    }

    void Widget::Afficher()
    {

    }
    void Widget::UpdateEvent()
    {

    }
}

